<template>
  <el-drawer
    :visible.sync="drawer"
    direction="rtl"
    size="50%"
    :with-header="false"
    :width="700"
    :append-to-body="true"
  >
    <div class="drawer_box">
      <div class="drawer_title">
        <div class="drawer_t">
          <div class="drawer_t_l">
            <img
              class="drawer_t_l_img"
              src="/static/skin/herodoo/main/platform/home/img/piliangoumai.png"
              alt=""
            />
            创建主题活动
          </div>
          <img
            class="drawer_t_r"
            src="/static/skin/herodoo/main/platform/home/img/guanbi.png"
            alt=""
            @click="drawer = false"
          />
        </div>
      </div>
      <div class="drawer_content">
        <div class="list-box">
          <div class="left">主题：</div>
          <div class="right">
            <el-select v-model="form.city" clearable placeholder="主题">
              <el-option
                v-for="item in cityList.options"
                :key="item.ID"
                :label="item.Name"
                :value="item.ID"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="list-box">
          <div class="left">主题活动名称：</div>
          <div class="right">
            <el-input
              v-model="form.title"
              placeholder="请输入主题活动名称"
            ></el-input>
          </div>
        </div>
        <div class="list-box">
          <div class="left">主题活动地图:</div>
          <div class="right">
            <div class="tips">
              可上传1张图片，图片不超过5MB，仅支持jpg、png格式
            </div>
            <imgUplode
              v-if="mapData.url == ''"
              :list="form.fileList"
              @imgList="getImgsUrl"
              :octopus="octopus"
              num="1"
              size="5120"
            >
            </imgUplode>
            <div></div>
          </div>
        </div>
        <div class="list-box" v-if="mapData.url != ''">
          <div class="left"></div>
          <div class="right boxinfo">
            <img class="big-img" :src="mapData.url" />
            <div
              v-for="(item, i) in mapData.list"
              :key="i"
              :style="{
                top: item.sy + 'px',
                left: item.sx + 'px',
                width: item.pw + 'px'
              }"
              :class="`small-box img${i + 1}`"
            >
              <img
                class="small"
                src="/static/skin/default/MCS/AIGC/Inventory/images/point1.png"
                alt=""
              />
              <span>{{ i + 1 }}</span>
            </div>
          </div>
        </div>
        <div class="list-box">
          <div class="left">地图打点:</div>
          <div class="right">
            <div class="add_content" @click="openPoint">
              <i class="el-icon-plus"></i>
              <span>去打点</span>
            </div>
          </div>
        </div>
        <div class="list-box">
          <div class="left"></div>
          <div class="right">
            <div v-for="(item, i) in mapData.list" :key="item.id">
              <div class="row">
                <div class="point">
                  <img
                    src="/static/skin/default/MCS/AIGC/Inventory/images/point1.png"
                  />
                  <span>{{ i + 1 }}</span>
                </div>
                <span class="name">{{ item.Name }}</span>
                <span class="lat"
                  >左边距: <span>{{ item.x }}</span> 像素</span
                >
                <span class="lat"
                  >上边距: <span>{{ item.y }}</span> 像素</span
                >
              </div>
            </div>
          </div>
        </div>
        <div class="list-box">
          <div class="left">打卡顺序:</div>
          <div class="right">
            <el-radio-group v-model="form.isRule" @input="changeRadio">
              <el-radio label="1">无序打卡</el-radio>
              <el-radio label="2">顺序打卡</el-radio>
            </el-radio-group>
            <div class="small">
              无序打卡:小程序用户无需按照添加打卡点的顺序进行打卡
            </div>
            <div class="small">
              顺序打卡:小程序用户打卡时将必须按照添加打卡点的顺序进行打卡，否则无法打卡。
            </div>
          </div>
        </div>

        <!-- <div class="list-box">
          <div class="left">打卡方式:</div>
          <div class="right">
            <el-select
              v-model="form.typeid"
              clearable
              placeholder="请选择打卡方式"
            >
              <el-option
                v-for="item in wayList"
                :key="item.ID"
                :label="item.Name"
                :value="item.ID"
              >
              </el-option>
            </el-select>
          </div>
        </div> -->

        <div class="list-box">
          <div class="left">主题活动勋章:</div>
          <div class="right">
            <el-select
              v-model="form.medal"
              clearable
              placeholder="请选择主题活动勋章"
            >
              <el-option
                v-for="item in medalList"
                :key="item.ID"
                :label="item.Name"
                :value="item.ID"
              >
              </el-option>
            </el-select>
          </div>
        </div>

        <div class="list-box">
          <div class="left">线下领奖地址:</div>
          <div class="right">
            <el-input
              v-model="form.address"
              placeholder="请输入线下领奖地址"
            ></el-input>
          </div>
        </div>

        <div class="list-box">
          <div class="left">线下领奖点经度:</div>
          <div class="right">
            <el-input
              v-model="form.XAxis"
              placeholder="请输入线下领奖点经度"
            ></el-input>
          </div>
        </div>

        <div class="list-box">
          <div class="left">线下领奖点纬度:</div>
          <div class="right">
            <el-input
              v-model="form.YAxis"
              placeholder="请输入线下领奖点纬度"
            ></el-input>
          </div>
        </div>

        <div class="list-box">
          <div class="left">主题活动详情：</div>
          <div class="right">
            <quill
              ref="quill"
              v-if="drawer"
              :octopus="octopus"
              :desc="form.deltedContent"
              @quillContent="getQuillContent"
            ></quill>
          </div>
        </div>
      </div>

      <div class="button_list">
        <button @click="save(1)">保存</button>
        <button @click="save(2)">发布</button>
      </div>
    </div>

    <MapView
      v-show="mapShow"
      ref="mapview"
      @close="close"
      @imgList="getImgsUrl"
    ></MapView>
  </el-drawer>
</template>

<script>
import imgUplode from "@/components/Components/imgUpdate.vue";
import quill from "@/components/Components/quillEditor.vue";

import MapView from "./MapView.vue";
export default {
  name: "cat_dialog",
  components: {
    imgUplode,
    quill,
    MapView
  },
  props: {},
  data() {
    return {
      octopus: this.$parent.octopus,
      drawer: false,
      isEdit: "look",
      form: {
        city: "", //所选城市
        title: "", //名称
        fileList: [], //地图
        pointList: [], // 打卡点
        isRule: "0", //打卡规则
        medal: "", //勋章
        address: "", //领奖地址
        deltedContent: "", //线路详情
        XAxis: "",
        YAxis: ""
      },
      rules: {
        city: { required: true, message: "请选择主题活动主题" },
        title: { required: true, message: "请输入主题活动名称" },
        fileList: { required: true, message: "请上传主题活动地图" },
        pointList: { required: true, message: "请添加至少一个打卡点" },
        medal: { required: false, message: "请上传主题活动勋章" },
        address: { required: true, message: "请输入线下领奖地址" },
        XAxis: { required: true, message: "请输入线下领奖地址经度" },
        YAxis: { required: true, message: "请输入线下领奖地址纬度" },
        deltedContent: { required: true, message: "请输入主题活动详情" }
      },
      cityList: {
        options: [],
        value: ""
      },
      wayList: [],
      medalList: [],
      mapShow: false,
      mapData: {
        url: "",
        list: []
      },
      boxInfo: {
        w: "",
        h: ""
      },
      oldCityWalk: ""
    };
  },
  created() {
    this.api_1301();
    this.api_1314();
    // this.getWay_1298();
  },
  watch: {},
  methods: {
    drawerOpen(data = null, type = "CREATE") {
      // editType 是否可编辑，打他 父级传的数据，type  是新建还是更新
      this.drawer = true;
      this.isEdit = type;

      if (!data) {
        for (let [k, v] of Object.entries(this.form)) {
          typeof v == "string" ? (k = "") : (k = []);
        }
        return;
      } else {
        this.form.city = data.ThemeID;
        this.form.title = data.Name;
        this.form.fileList = JSON.parse(data.LineUrl);
        this.form.isRule = String(data.SortTypeID);
        this.form.address = data.Address;
        this.form.deltedContent = data.Description;
        this.form.XAxis = data.XAxis;
        this.form.YAxis = data.YAxis;
        this.form.ID = data.ID;
        this.medalList = [
          ...this.medalList,
          {
            ID: data.BadgeID,
            Name: data.BadgeName
          }
        ];

        let map = new Map();
        for (let item of this.medalList) {
          if (!map.has(item.ID)) {
            map.set(item.ID, item);
          }
        }

        this.medalList = [...map.values()];

        this.form.medal = data.BadgeID;

        this.oldCityWalk = JSON.parse(JSON.stringify(data.BadgeID));

        this.get_api_1303(data.ID, "search");
      }
    },
    openPoint() {
      // this.$refs.SelectPoint.drawerOpen();
      this.mapShow = true;
      this.$nextTick(() => {
        this.$refs.mapview.upSet(this.form.fileList, this.form.pointList);
      });
    },
    getQuillContent(val) {
      this.form.deltedContent = val;
    },
    getImgsUrl(val) {
      this.form.fileList = val;
      this.mapData.url = val[0].url;

      let img = new Image();
      img.src = this.mapData.url;
      img.onload = () => {
        let dom = document.querySelector(".boxinfo");
        this.boxInfo = {
          w: dom.offsetWidth,
          h: dom.offsetHeight
        };
      };
    },
    getMedal(val) {
      this.form.medal = val;
    },
    save(status = 1) {
      if (!this.octopus.tools.verify(this.form, this.rules)) {
        return;
      }
      this.$confirm(`确定保存这个城市活动吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        // type: 'warning'
        center: true
      })
        .then(res => {
          this.update_api_1291(status);
        })
        .catch(() => {
          console.log("取消了");
        });
    },
    // 保存线路
    update_api_1291(status) {
      let cellDate = [
        { name: "Address", defaultValue: this.form.address }, // 领奖地址
        { name: "Description", defaultValue: this.form.deltedContent }, // 活动详情
        { name: "LineUrl", defaultValue: JSON.stringify(this.form.fileList) }, // 大图
        { name: "Name", defaultValue: this.form.title }, // 名称
        { name: "Status", defaultValue: status }, // 路线状态（1未发布  2已发布  3 取消）
        { name: "ThemeID", defaultValue: this.form.city }, // 主题ID
        { name: "SortTypeID", defaultValue: this.form.isRule }, // 打卡顺序 （1无序  2顺序打卡）
        { name: "XAxis", defaultValue: this.form.XAxis }, // 经度
        { name: "YAxis", defaultValue: this.form.YAxis } // 纬度
      ];
      if (this.isEdit == "CREATE") {
        cellDate.push({
          name: "CreateTime",
          defaultValue:
            new Date().toJSON().substring(0, 10) +
            " " +
            new Date().toTimeString().substring(0, 8)
        });
        cellDate.push({
          name: "CreateUserID",
          defaultValue: this.octopus.passport.id
        });
      } else {
        cellDate.push({
          name: "UpdateTime",
          defaultValue:
            new Date().toJSON().substring(0, 10) +
            " " +
            new Date().toTimeString().substring(0, 8)
        });
        cellDate.push({
          name: "UpdateUserID",
          defaultValue: this.octopus.passport.id
        });
      }
      this.octopus.request.api.post(
        "/Octopus/Engine/Item",
        {
          id: 1291,
          type: this.isEdit,
          value: this.isEdit == "CREATE" ? "" : this.form.ID,
          cellData: cellDate,
          paramData: {}
        },
        data => {
          if (data.status.code == 200) {
            if (this.form.medal != this.oldCityWalk) {
              this.update_api_1282(this.oldCityWalk);
            }
            this.update_api_1282(this.form.medal, data.content.value);

            this.get_api_1303(data.content.value);
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    // 保存徽章
    update_api_1282(value, ID = "") {
      this.octopus.request.api.post(
        "/Octopus/Engine/Item",
        {
          id: 1282,
          type: "UPDATE",
          value: value, //勋章ID
          cellData: [
            { name: "CityWalkID", defaultValue: ID }, // CityWalkID
            {
              name: "UpdateTime",
              defaultValue:
                new Date().toJSON().substring(0, 10) +
                " " +
                new Date().toTimeString().substring(0, 8)
            }, // UpdateTime
            { name: "UpdateUserID", defaultValue: this.octopus.passport.id } // UpdateUserID
          ],
          paramData: {}
        },
        data => {
          if (data.status.code == 200) {
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    // 保存,删除大图上的点
    update_api_1289(type = "CREATE", ID, v, index) {
      this.octopus.request.api.post(
        "/Octopus/Engine/Item",
        {
          id: 1289,
          type: type,
          value: type == "CREATE" ? "" : ID,
          cellData: [
            {
              name: "CreateTime",
              defaultValue:
                new Date().toJSON().substring(0, 10) +
                " " +
                new Date().toTimeString().substring(0, 8)
            }, // 创建时间
            { name: "CreateUserID", defaultValue: this.octopus.passport.id }, // 创建用户
            { name: "PointID", defaultValue: type == "CREATE" ? (v.PointID ? v.PointID : v.ID) : "" }, // 点位表ID
            { name: "WalkLineID", defaultValue: ID }, // 打卡路线ID
            { name: "Sort", defaultValue: index }, // 排序
            { name: "XPoint", defaultValue: type == "CREATE" ? v.x : "" }, // XPoint
            { name: "YPoint", defaultValue: type == "CREATE" ? v.y : "" } // YPoint
          ],
          paramData: {}
        },
        data => {
          if (data.status.code == 200) {
            this.drawer = false;

            this.$emit("reload");
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    // 获取大图上的点
    get_api_1303(ID, type = "CREATE") {
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 1303,
          cellData: [
            { name: "CreateTime" }, // 创建时间
            { name: "CreateUserID" }, // 创建用户
            { name: "ID" }, // ID
            { name: "IsWorldMap" }, // IsWorldMap
            { name: "PointID" }, // 点位表ID
            { name: "Sort" }, // 排序
            { name: "WalkLineID" }, // 打卡路线ID
            { name: "x" }, // XPoint
            { name: "y" }, // YPoint
            { name: "Name" } // YPoint
          ],
          paramData: {
            WalkLineID: ID
          }
        },
        data => {
          if (data.status.code == 200) {
            if (type != "CREATE") {
              // 只获取 点位，渲染页面用
              let obj = this.form.fileList[0];
              let blw = obj.widthBl;
              let blh = obj.heightBl;

              data.content.queryData.map(v => {
                v.sx = v.x / blw;
                v.sy = v.y / blh;
                v.pw = 33 / blw;
              });

              data.content.queryData.sort((a, b) =>{
                return a.Sort - b.Sort;
              });

              this.form.pointList = data.content.queryData;
              this.mapData = {
                list: data.content.queryData,
                url: obj.url
              };

              let img = new Image();
              img.src = this.mapData.url;
              img.onload = () => {
                let dom = document.querySelector(".boxinfo");
                this.boxInfo = {
                  w: dom.offsetWidth,
                  h: dom.offsetHeight
                };
              };
              return;
            }
            let arr = data.content.queryData;
            //删除该 线路的所有点位
            arr.length > 0 &&
              arr.map(v => {
                this.update_api_1289("DELETE", v.ID);
              });
            // 创建 该线路的点位
            this.form.pointList.map((v, i) => {
              this.update_api_1289("CREATE", ID, v, i);
            });
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            // ;
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    getPoint(e) {
      this.form.lng = e.lng;
      this.form.lat = e.lat;
    },
    getPointList(e) {},
    close(list, bigImg) {
      let blw = bigImg.w / this.boxInfo.w;
      let blh = bigImg.h / this.boxInfo.h;
      list.map(v => {
        v.sx = v.x / blw;
        v.sy = v.y / blh;
        v.pw = 33 / blw;
        v.w_bl = v.x / bigImg.w;
        v.h_bl = v.y / bigImg.h;
      });
      this.mapData.list = this.form.pointList = list;
      this.form.fileList[0].width = bigImg.w;
      this.form.fileList[0].height = bigImg.h;
      this.form.fileList[0].widthBl = blw;
      this.form.fileList[0].heightBl = blh;

      this.mapShow = false;
    },
    //获取城市活动类型
    api_1301() {
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 1301, // 菜单表的八爪鱼ID
          cellData: [
            { name: "Name" }, // 主题名称
            { name: "IsTopic" }, // 是否置顶
            { name: "count" }, // 包含城市活动数量
            { name: "StartTime" }, // 主题开始时间
            { name: "EndTime" }, // 主题结束时间
            { name: "UpdateUserIDName" }, // 操作人
            { name: "Address", show: false }, // 主题地址
            { name: "Description", show: false }, // 主题介绍
            { name: "ID", show: false }, // ID
            { name: "PosterPath", show: false }, // 海报
            { name: "PvCount", show: false }, // 主题浏览量
            { name: "Sort", show: false }, // Sort
            { name: "UpdateTime", show: false }, // 操作时间
            { name: "UpdateUserID", show: false } // 操作人
          ]
        },
        // 处理回调事件，通过分析data.status.code的状态来实现。
        data => {
          if (data.status.code === 200) {
            this.cityList.options = data.content.queryData;
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    api_1314() {
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 1314, // 菜单表的八爪鱼ID
          cellData: [
            { name: "ID", show: false }, // ID
            { name: "Name" }, // 勋章名称
            { name: "TypeID", show: false }, // 获取方式
            { name: "TypeIDName", width: 150 }, // TypeIDName
            { name: "RuleID", show: false }, // 获取规则id
            { name: "RuleIDName", width: 300 }, // RuleIDName
            { name: "Status" }, // 勋章状态
            { name: "UpdateUserIDName" }, // 操作人名称
            { name: "UpdateTime", width: 160 }, // 操作时间
            { name: "PicturePath", show: false }, // 勋章图片
            { name: "StatusID", show: false }, // StatusID
            { name: "UpdateUserID", show: false }, // 操作人id
            { name: "UpdateUserIDName", show: false } // 操作人名称
          ],
          paramData: {
            TypeID: 1
          }
        },
        // 处理回调事件，通过分析data.status.code的状态来实现。
        data => {
          if (data.status.code === 200) {
            this.medalList = data.content.queryData;
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    },
    getWay_1298() {
      this.octopus.request.api.post(
        "/Octopus/Engine/List",
        {
          id: 1298,
          cellData: [
            { name: "ID" }, // ID
            { name: "Name" }, // Name
            { name: "ParentkeyValue" } // ParentkeyValue
          ],
          paramData: {}
        },
        data => {
          if (data.status.code == 200) {
            this.wayList = data.content.queryData;
          } else {
            this.octopus.tips.error(data);
          }
        }
      );
    }
  }
};
</script>
<style scoped>
.drawer_box {
  padding: 3rem 0.8rem 3rem 0;
  height: calc(100% - 5rem);
  display: flex;
  flex-direction: column;
}

.drawer_title {
  box-shadow: 0px 5px 5px rgb(230 228 228 / 30%);
  padding-bottom: 1rem;
  font-size: 1.6rem;
}

.drawer_t {
  padding: 0 2.8rem;
  display: flex;
  justify-content: space-between;
}

.drawer_t_l {
  display: flex;
  align-items: center;
  font-size: 2rem;
  font-weight: bold;
}

.drawer_t_l_img {
  width: 2.8rem;
  margin-right: 1rem;
}

.drawer_t_r {
  width: 3rem;
  height: 3rem;
  cursor: pointer;
}

.button_box {
  margin-top: 1rem;
  padding: 0 3rem;
  display: flex;
}

.drawer_content {
  overflow-y: auto;
  margin-top: 1.5rem;
  height: calc(100vh - 11rem);
  padding: 0 3rem;
  width: 100%;
  padding-bottom: 1rem;
}

.drawer_content .title {
  font-size: 1.8rem;
  font-weight: bold;
}

.drawer_content .driver-title {
  margin-top: 1em;
}

.content-box {
  display: flex;
  align-items: center;
  margin: 2rem 0 2rem 2em;
}

.mt-10 {
  margin-top: 10rem;
}

.content-box .label {
  width: 25%;
  font-size: 1.6rem;
}

.content-box .label .label-bold {
  font-size: 1.8rem;
  font-weight: bold;
}

.content-box .desc {
  flex: 1;
  font-size: 1.6rem;
}

.list-box {
  display: flex;
  width: 100%;
  justify-content: space-between;
}

.list-box {
  line-height: 4rem;
  margin-top: 2rem;
}

.list-box .left {
  width: 20%;
  font-size: 1.5rem;
}

.list-box .right {
  width: 80%;
  position: relative;
}

.button_list {
  margin-top: 1rem;
  display: flex;
  justify-content: end;
  position: absolute;
  background-color: #ffff;
  bottom: 1rem;
  width: calc(100% - 7rem);
}

.button_list button {
  border: none;
  height: 4rem;
  width: 12rem;
  border-radius: 1rem;
  margin-left: 1rem;
  color: #ffff;
  background-color: #f78c53;
  cursor: pointer;
}

.list-box .right .add_content {
  height: 4.5rem;
  line-height: 4.5rem;
  border: 1px dashed #f87338;
  display: inline-block;
  padding: 0 1.5rem;
  cursor: pointer;
  border-radius: 0.5rem;
}

.list-box .right .el-input {
  width: 100%;
  height: 4.5rem;
}

.list-box .right .add_content i {
  color: #f87338;
  font-weight: 700;
  margin-right: 0.5rem;
}
.small {
  font-size: 1rem;
  color: #f87338;
}
.big-img {
  width: 100%;
}
.row {
  display: flex;
  flex-direction: row;
  align-items: center;
  border-bottom: 1px solid #f1f1f1;
}
.row .name {
  margin-right: 10px;
  min-width: 10vw;
}
.point {
  width: 24px;
  height: 24px;
  position: relative;
  margin-right: 10px;
}
.point img {
  width: 100%;
}
.point span {
  position: absolute;
  top: 0;
  left: 50%;
  line-height: 100%;
  transform: translate(-50%, 16%);
  color: #fff;
  display: inline-block;
}
.row .lat {
  min-width: 8vw;
  display: inline-block;
}
.row .lat span {
  padding: 0 10px;
}
.small-box {
  position: absolute;
  top: 0;
  left: 0;
  color: #fff;
}
.small {
  width: 100%;
}
.small-box span {
  position: absolute;
  top: 0%;
  left: 50%;
  transform: translate(-50%, -28%);
}
</style>
